import time

from django.db import models

from Channel import Channel

class Action(models.Model):
    class Meta:
        app_label = 'chat'

    ACTIONS = (
        # User Actions
        (u'CONNECT', u'Connect'),
        (u'DISCONNECT', u'Disconnect'),
        (u'JOIN', u'Join'),
        (u'PART', u'Part'),
        (u'TALK', u'Talk'),
        (u'SELFTALK', u'Self Talk'),
        (u'PRIVATETALK', u'Private Talk'),
        
        # Administrator Actions
        (u'KICK', u'Kick'),
        (u'BAN', u'BAN'),
    )
    
    action = models.CharField(max_length=32, choices=ACTIONS)
    timestamp = models.DateTimeField(auto_now=True)
    username = models.CharField(max_length=256)
    username_id = models.CharField(max_length=256)
    data = models.CharField(max_length=1024)

    target = models.ForeignKey(Channel, blank=True, null=True)
    
    @classmethod
    def get_json(cls, o):
        return {
                "id": o.id,
                "action": o.action,
                "timestamp": time.mktime(o.timestamp.timetuple()),
                "username": o.username,
                "username_id": o.username_id,
                "target": o.target.hash,
                "data": o.data,
                }
    
    def __unicode__(self):
        return "%s %s %s %s %s" % (self.timestamp, self.action, self.username, self.target.hash, self.data)
